package com.sgb.member.ui.fragment;

import android.os.Bundle;
import android.text.TextUtils;
import android.util.SparseArray;
import android.util.SparseIntArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.google.android.material.tabs.TabLayout;
import com.sgb.member.PickCateType;
import com.sgb.member.R;
import com.sgb.member.databinding.FragmentMemberCollectingBinding;
import com.sgb.member.di.DaggerMemberFragComponent;
import com.sgb.member.di.MemberFragModule;
import com.sgb.member.entity.MemberCentralizedEntity;
import com.sgb.member.entity.MemberCentralizedFactoryEntity;
import com.sgb.member.ui.activity.MemberBusinessActivity;
import com.sgb.member.ui.adapter.HomeOtherMemberAdapter;
import com.sgb.member.view_model.BusinessCollectingModel;
import com.swgk.core.base.AppLazyFragment;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.customview.XCustomTabLayout;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;
import com.swgk.core.recyclerview.decoration.RecyclerCustomDecoration;
import com.swgk.core.view.areapopup.AddressEntity;
import com.swgk.core.view.areapopup.GoodsSourceLocationEntity;
import com.swgk.core.view.areapopup.ThreeLevelAreaWindow;
import com.swgk.core.view.selection.CommonPopEntity;
import com.swgk.core.view.selection.CopyUtils;
import com.swgk.core.view.selection.MemberStatusView;
import com.swgk.core.view.selection.XChooseDialog;
import com.swgk.core.view.selection.XChooseEntity;
import com.swgk.core.xrecyclerview.LoadingMoreFooter;
import com.swgk.core.xrecyclerview.XRecyclerView;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import javax.inject.Inject;

public class BusinessCollectingFragment extends AppLazyFragment implements MemberStatusView.OnChooseListener, XChooseDialog.DialogCallBackImpl {

    @Inject
    public BusinessCollectingModel sModel;
    private FragmentMemberCollectingBinding mBinding;
    private HomeOtherMemberAdapter<MemberCentralizedEntity> mAdapter;
    private HomeOtherMemberAdapter<MemberCentralizedFactoryEntity> mAdapterGF;
    private int mCurPos; // 主页面TAB下标
    private CommonPopEntity mCurEntity; // 主页面TAB下标
    // 经营地址Popup
    private ThreeLevelAreaWindow mAddressPop;
    private List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>> scrData;

    private String mNo0 = ""; // 区分 需方发起（1）、供方发起（”“）、企业专项（2）
    private String mNo1 = ""; // 1, "集采方式");
    private String mNo2 = ""; // 2, "集采状态");
    private String mNo3 = ""; // 3, "集采类型");
    private String mNo31 = ""; // 31-35, "");
    private String mNo32 = ""; // 31-35, "");
    private String mNo33 = ""; // 31-35, "");
    private String mNo4 = ""; // 4, "报价客户");
    private String mNo5 = ""; // 5, "加盟客户");
    private String mNo6 = ""; // 6, "截至时间");
    private String mNo7 = ""; // 7, "城市");
    private String mNo8 = ""; // 8, "市区");
    private String member_level = "";//会员等级
    private List<CommonPopEntity> mCollectTypes;

    public static BusinessCollectingFragment newInstance(int position, CommonPopEntity entity) {
        BusinessCollectingFragment fragment = new BusinessCollectingFragment();
        Bundle bundle = new Bundle();
        bundle.putInt("INDEX", position);
        bundle.putSerializable("ENTITY", entity);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    protected void setupFragmentComponent(AppComponent appComponent) {
        DaggerMemberFragComponent.builder()
                .appComponent(appComponent)
                .memberFragModule(new MemberFragModule(this))
                .build()
                .inject(this);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mBinding = DataBindingUtil.inflate(inflater, R.layout.fragment_member_collecting, container, false);
        mBinding.setLifecycleOwner(this);
        return mBinding.getRoot();
    }

    @Override
    protected void initView() {
        mCurPos = getArguments().getInt("INDEX", 0);
        mCurEntity = (CommonPopEntity) getArguments().getSerializable("ENTITY");
        mBinding.recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        mBinding.chooseLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                for (int i = 0; i < mNameArray.size(); i++) {
                    XChooseEntity chooseEntity = mData.get(mData.keyAt(i));
                    if (chooseEntity != null) {
                        if (chooseEntity.getList() == null || chooseEntity.getList().size() == 0) {
                            int sort = chooseEntity.getSort();
                            switch (mCurPos) {
                                case 0: // 需方集采
                                    if (sort == PickCateType.TYPE_1 || sort == PickCateType.TYPE_2 || sort == PickCateType.TYPE_7) {
                                        loadOtherData();
                                        return;
                                    }
                                    break;
                                case 1: // 供方发起
                                    if (sort == PickCateType.TYPE_2 || sort == PickCateType.TYPE_31 || sort == PickCateType.TYPE_6 || sort == PickCateType.TYPE_7) {
                                        loadOtherData();
                                        return;
                                    }
                                    break;
                                case 2: // 企业专项
                                    if (sort == PickCateType.TYPE_1 || sort == PickCateType.TYPE_2 || sort == PickCateType.TYPE_4 || sort == PickCateType.TYPE_5 || sort == PickCateType.TYPE_7) {
                                        loadOtherData();
                                        return;
                                    }
                                    break;
                            }
                        }
                    }
                }
                mChooseDialog.initData(mData, mFilters);
                mChooseDialog.showDialog();
            }
        });
        initChooseDialog();
        initPopup();
        initRecycler();
    }

    @Override
    protected void onLazyLoad() {
        if (mCurPos == 1) {
            if (isListEmpty(PickCateType.TYPE_31)) {
                // 集采类型 一级分类
                sModel.getTenderGoodsCategoryAllList(PickCateType.TYPE_31, mNo3, false); // 集采类型
            }
        } else {
            // 集采类型分类
            sModel.getGroupBuyClassList(PickCateType.TYPE_3, false); // 集采类型
        }
        loadData(false);

    }

    public void loadData(boolean isLoadMore) {
        String text = ((MemberBusinessActivity) getActivity()).getSearchText();
        if (mCurPos == 1) {
            sModel.groupBuyGFList(isLoadMore, text, mNo0, mNo1, mNo2, mNo3, mNo31, mNo32, mNo33, mNo4, mNo5, mNo6, mNo7, mNo8,member_level);
        } else {
            sModel.groupBuyList(isLoadMore, text, mNo0, mNo1, mNo2, mNo3, mNo31, mNo4, mNo5, mNo6, mNo7, mNo8,member_level);
        }
    }

    private void loadOtherData() {
        switch (mCurPos) {
            case 0: // 需方集采
                if (isListEmpty(PickCateType.TYPE_1)) {
                    sModel.getCollectingMode(PickCateType.TYPE_1, false); // 集采方式
                }
                if (isListEmpty(PickCateType.TYPE_2)) {
                    sModel.getStatus(PickCateType.TYPE_2, false); // 集采状态
                }
                break;
            case 1: // 供方发起
                if (isListEmpty(PickCateType.TYPE_2)) {
                    sModel.getStatus(PickCateType.TYPE_2, false); // 集采状态
                }
                if (isListEmpty(PickCateType.TYPE_31)) {
                    // 集采类型
                    sModel.getTenderGoodsCategoryAllList(PickCateType.TYPE_31, mNo3, false); // 集采类型
                }
                if (isListEmpty(PickCateType.TYPE_6)) {
                    sModel.showTimePopWindow(PickCateType.TYPE_6, false); // 截至时间
                }
                break;
            case 2: // 企业专项
                if (isListEmpty(PickCateType.TYPE_1)) {
                    sModel.getCollectingMode(PickCateType.TYPE_1, false); // 集采方式
                }
                if (isListEmpty(PickCateType.TYPE_2)) {
                    sModel.getStatus(PickCateType.TYPE_2, false); // 集采状态
                }
                if (isListEmpty(PickCateType.TYPE_4)) {
                    sModel.getOfferList(PickCateType.TYPE_4, false); // 报价客户
                }
                if (isListEmpty(PickCateType.TYPE_5)) {
                    sModel.getJoinTypeList(PickCateType.TYPE_5, false); // 加盟客户
                }
                break;
        }

        if (isListEmpty(PickCateType.TYPE_7)) {
            sModel.getAddressModel(PickCateType.TYPE_7);  // 经营地址
        }
        if (isListEmpty(PickCateType.TYPE_9)) {
            sModel.getMemberLevelData(); // 会员级别
        }
    }
    private List<CommonPopEntity> list;
    public void setMemberLevelData(List<CommonPopEntity> list) {
        this.list = list;
        addMainEntity(PickCateType.TYPE_9, list);
    }

    public void setAddressData(List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>> data) {
        if (data != null && data.size() > 0) {
            scrData = data;
            if (TextUtils.isEmpty(mNo7)) {
                mAddressPop.setData(data, true);
            } else {
                mAddressPop.setDataAndPosition(data, mNo7, mNo8, "", true, true);
            }
        }
    }

    int category = 1;

    public void updateTabLayout(List<CommonPopEntity> data) {
        if (data == null || data.size() == 0) {
            mBinding.tabLayout.setVisibility(View.GONE);
            showEmpty();
            return;
        }
        mBinding.tabLayout.setVisibility(View.VISIBLE);
        mCollectTypes = data;
        mBinding.tabLayout.clearOnTabSelectedListeners();
        mBinding.tabLayout.removeAllTabs();
        for (int i = 0; i < data.size(); i++) {
            mBinding.tabLayout.addTab(mBinding.tabLayout.newTab().setText(data.get(i).getTypeName()));
        }
        mBinding.tabLayout.setNormalColor(R.color.color_title_small);
        mBinding.tabLayout.addOnTabSelectedListener(data, new XCustomTabLayout.SimpleSelectedListener() {

            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                int position = tab.getPosition();
                String no = mCollectTypes.get(position).getNo();
                if (!TextUtils.equals(no, mNo3)) {
                    mNo3 = no;
                    mNo31 = "";
                    mNo32 = "";
                    mNo33 = "";
                    for (Integer integer : mCateOther) {
                        addMainEntity(integer, new ArrayList<>());
                    }
                    if (position == 0) {
                        updateChildTabLayout(new ArrayList<>(), 0);
                    } else {
                        sModel.getTenderGoodsCategoryAllList(PickCateType.TYPE_31, mNo3, false); // 集采类型
                    }
                    loadData(false);
                }
            }
        });
    }

    private void updateChildTabLayout(List<CommonPopEntity> data, int index) {
        if (data == null || data.size() == 0) {
            mBinding.childTab.setVisibility(View.GONE);
            return;
        }
        mBinding.childTab.setVisibility(View.VISIBLE);
        mBinding.childTab.removeAllTabs();
        mBinding.childTab.clearOnTabSelectedListeners();
        for (int i = 0; i < data.size(); i++) {
            mBinding.childTab.addTab(mBinding.childTab.newTab().setText(data.get(i).getTypeName()));
        }
        mBinding.childTab.scrollTo(0, 0);
        mBinding.childTab.setNormalColor(R.color.color_title_small);
        mBinding.childTab.addOnTabSelectedListener(data, new XCustomTabLayout.SimpleSelectedListener() {

            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                if (isTabTwoDialogRefresh) {
                    return;
                }
                int pos = tab.getPosition();
                XChooseEntity chooseEntity = mData.get(PickCateType.TYPE_31);
                mNo31 = chooseEntity.getItemNo(pos);
                mFilters.put(PickCateType.TYPE_31, pos);
                mNo32 = "";
                mNo33 = "";
                updateMultiCategoryData(chooseEntity.getSort(), chooseEntity.getList().get(pos), pos, false);
                loadData(false);
            }
        });
        if (index > 0) {
            mBinding.childTab.postDelayed(new Runnable() {
                @Override
                public void run() {
                    Objects.requireNonNull(mBinding.childTab.getTabAt(index)).select();
                    isTabTwoDialogRefresh = false;
                }
            }, 10);
        }
        category = !TextUtils.isEmpty(mNo3) ? Integer.parseInt(mNo3) : 1;
        String name1 = titles[category - 1][0];
        String name2 = titles[category - 1][1];
        String name3 = titles[category - 1][2];
        mData.get(PickCateType.TYPE_31).setTypeName(name1);
        mData.get(PickCateType.TYPE_32).setTypeName(name2);
        mData.get(PickCateType.TYPE_33).setTypeName(name3);
    }


    /**
     * ------------------------------- Popup ---------------------------------------------
     */

    private void initPopup() {
        // mNo0 1,需方集采，2企业专项
        mBinding.popAddress.setVisibility(View.VISIBLE);
        mBinding.popStatus.setVisibility(View.VISIBLE);
        switch (mCurPos) {
            case 0:
                mNo0 = "1";
                mBinding.popMode.setVisibility(View.VISIBLE);
                break;
            case 1: // 供方发起 只有商品分类
                mNo0 = "";
                mNo3 = "1"; //默认商品分类
                mBinding.popTime.setVisibility(View.VISIBLE);
                mBinding.tabLayout.setVisibility(View.GONE);
                break;
            case 2:
                mNo0 = "2";
                mBinding.popMode.setVisibility(View.VISIBLE);
                break;
        }
        mBinding.popStatus.initPopup(mBinding.grayLayout, mBinding.popLine, this);
        mBinding.popMode.initPopup(mBinding.grayLayout, mBinding.popLine, this);
        mBinding.popTime.initPopup(mBinding.grayLayout, mBinding.popLine, this);
        initAddressPopup();
    }

    private void initAddressPopup() {
        mBinding.popAddress.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (scrData == null) {
                    sModel.getAddressModel(PickCateType.TYPE_7);
                    return;
                }
                mBinding.popLine.setVisibility(View.VISIBLE);
                if (!TextUtils.isEmpty(mNo7) || !TextUtils.isEmpty(mNo8)) {
                    mBinding.popAddress.updateChooseTextStatus(-2, null);
                } else {
                    mBinding.popAddress.updateChooseTextStatus(-1, mBinding.popAddress.getKeyName());
                }
                mAddressPop.showPopwindow0(mBinding.popLine);
            }
        });
        mAddressPop = new ThreeLevelAreaWindow(getActivity());
        mAddressPop.setBgView(mBinding.grayLayout);
        mAddressPop.setMemberHeight();
        mAddressPop.setCitySingle();
        mAddressPop.setProvinceSingle();
        mAddressPop.setShowSimpleName(true);
        mAddressPop.setOnlyShowProAndCity(true);
        mAddressPop.setAllSingleSelect(true);
        mAddressPop.setThreeListIsGone(true);
        mAddressPop.setOnResultClick(new ThreeLevelAreaWindow.OnResultClickProxy() {
            @Override
            protected void result(GoodsSourceLocationEntity entity) {
                mNo7 = "";
                mNo8 = "";
                mFilters.put(PickCateType.TYPE_7, mAddressPop.getProvincePosition());
                addMainEntity(PickCateType.TYPE_8, new ArrayList<>());
                mBinding.popAddress.updateChooseTextStatus(0, mBinding.popAddress.getKeyName());
                if (!TextUtils.isEmpty(entity.getProvinceNo())) {
                    mNo7 = entity.getProvinceNo();
                    mBinding.popAddress.updateChooseTextStatus(1, entity.getProvinceName());
                    mFilters.put(PickCateType.TYPE_7, mAddressPop.getProvincePosition());
                    if (!TextUtils.isEmpty(entity.getCityNo())) {
                        mNo8 = entity.getCityNo();
                        mBinding.popAddress.updateChooseTextStatus(1, entity.getCityName());
                        sModel.getAddressCityData(scrData, mNo7, PickCateType.TYPE_8, false);
                        mFilters.put(PickCateType.TYPE_8, mAddressPop.getmCityPosition());
                    }
                }
                //刷新数据
                loadData(false);
            }
        });
        mAddressPop.setOnDismissListener(() -> {
            mBinding.popLine.setVisibility(View.GONE);
            if (!TextUtils.isEmpty(mNo7) || !TextUtils.isEmpty(mNo8)) {
                mBinding.popAddress.updateChooseTextStatus(1, null);
            } else {
                mBinding.popAddress.updateChooseTextStatus(0, mBinding.popAddress.getKeyName());
            }
        });
    }

    @Override
    public void onLoadChooseData(String mKeyName) {
        switch (mKeyName) {
            case "集采方式":
                sModel.getCollectingMode(PickCateType.TYPE_1, false);
                break;
            case "集采状态":
                sModel.getStatus(PickCateType.TYPE_2, false);
                break;
            case "截止时间":
                sModel.showTimePopWindow(PickCateType.TYPE_6, false);
                break;
        }
    }

    @Override
    public void onChooseResult(String mKeyName, CommonPopEntity entity, Object... objects) {
        int position = (int) objects[0];
        String no = position == 0 ? "" : entity.getNo();
        String id = position == 0 ? "" : entity.getId();
        String name = position == 0 ? "" : entity.getTypeName();
        switch (mKeyName) {
            case "集采方式":
                if (!TextUtils.equals(no, mNo1)) {
                    mNo1 = no;
                    mFilters.put(PickCateType.TYPE_1, position);
                    loadData(false);
                }
                break;
            case "集采状态":
                if (!TextUtils.equals(no, mNo2)) {
                    mNo2 = no;
                    mFilters.put(PickCateType.TYPE_2, position);
                    loadData(false);
                }
                break;
            case "截止时间":
                if (!TextUtils.equals(no, mNo6)) {
                    mNo6 = no;
                    mFilters.put(PickCateType.TYPE_6, position);
                    loadData(false);
                }
                break;
        }
    }

    /**
     * -------------------------------------initRecycler------------------------------------------
     */
    private void initRecycler() {
        TextView textView = mBinding.emptyView.findViewById(R.id.no_data_count);
        textView.setVisibility(View.VISIBLE);
        mBinding.recyclerView.setPullRefreshEnabled(true);
        mBinding.recyclerView.setLoadingMoreEnabled(true);
        LoadingMoreFooter footer = (LoadingMoreFooter) mBinding.recyclerView.getFootView();
        footer.setNoMoreHint("已经到底啦~");
        mBinding.recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        mBinding.recyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                loadData(false);
            }

            @Override
            public void onLoadMore() {
                loadData(true);
            }
        });
        initRecyclerAdapter();
    }

    private void initRecyclerAdapter() {
        mBinding.recyclerView.addItemDecoration(new RecyclerCustomDecoration(0, 0, 0, getResources().getDimension(R.dimen.dp1)));
        if (mCurPos == 1) {
            mAdapterGF = new HomeOtherMemberAdapter<>(getActivity(), R.layout.item_other_centralized_factory, null);
            mAdapterGF.onIChildClick(new HomeOtherMemberAdapter.IChildClick<MemberCentralizedFactoryEntity>() {
                @Override
                public void onClick(MemberCentralizedFactoryEntity entity) {
                    sModel.getH5UrlFactory(entity.getNo());
                }
            });
            mAdapterGF.setOnItemClick(new AdapterOnItemClick<MemberCentralizedFactoryEntity>() {
                @Override
                public void onItemClick(MemberCentralizedFactoryEntity entity, int position) {
                    sModel.getH5UrlFactory(entity.getNo());
                }
            });
            mBinding.recyclerView.setAdapter(mAdapterGF);
        } else {
            mAdapter = new HomeOtherMemberAdapter<>(getActivity(), R.layout.item_other_centralized_mining, null);
            mAdapter.onIChildClick(new HomeOtherMemberAdapter.IChildClick<MemberCentralizedEntity>() {
                @Override
                public void onClick(MemberCentralizedEntity entity) {
                    sModel.getH5Url(entity.getNo());
                }
            });
            mAdapter.setOnItemClick(new AdapterOnItemClick<MemberCentralizedEntity>() {
                @Override
                public void onItemClick(MemberCentralizedEntity entity, int position) {
                    sModel.getH5Url(entity.getNo());
                }
            });
            mBinding.recyclerView.setAdapter(mAdapter);
        }

    }

    public void closeRecyclerRefreshing() {
        mBinding.recyclerView.refreshComplete();
        mBinding.recyclerView.loadMoreComplete();
    }

    public void showEmpty() {
        if (mCurPos == 1) {
            mAdapterGF.clearDatas();
        } else {
            mAdapter.clearDatas();
        }
        mBinding.emptyView.setVisibility(View.VISIBLE);
    }

    public void refreshRecycler(List<MemberCentralizedEntity> list, boolean isLoadMore, boolean noMoreData) {
        mBinding.recyclerView.setVisibility(View.VISIBLE);
        mBinding.recyclerView.setNoMore(noMoreData);
        mAdapter.setType(TextUtils.isEmpty(mNo1) ? 0 : 1);
        closeRecyclerRefreshing();
        if (isLoadMore) {
            if (list != null && list.size() > 0) {
                mAdapter.addDatas(list);
            }
        } else {
            if (list != null && list.size() > 0) {
                mBinding.emptyView.setVisibility(View.GONE);
                mAdapter.setDatas(list);
            } else {
                mAdapter.clearDatas();
                mBinding.emptyView.setVisibility(View.VISIBLE);
            }
        }
    }

    public void refreshRecyclerGF(List<MemberCentralizedFactoryEntity> list, boolean isLoadMore, boolean noMoreData) {
        mBinding.recyclerView.setVisibility(View.VISIBLE);
        mBinding.recyclerView.setNoMore(noMoreData);
        mAdapterGF.setType(TextUtils.isEmpty(mNo1) ? 0 : 1);
        closeRecyclerRefreshing();
        if (isLoadMore) {
            if (list != null && list.size() > 0) {
                mAdapterGF.addDatas(list);
            }
        } else {
            if (list != null && list.size() > 0) {
                mBinding.emptyView.setVisibility(View.GONE);
                mAdapterGF.setDatas(list);
            } else {
                mAdapterGF.clearDatas();
                mBinding.emptyView.setVisibility(View.VISIBLE);
            }
        }
    }


    /**
     * ------------------------------------ 侧滑刷选 ----------------------------------------
     */

    private XChooseDialog mChooseDialog;
    // 数据集
    private SparseArray<XChooseEntity> mData = new SparseArray<>();
    // 本地缓存筛选结果
    private SparseIntArray mFilters = new SparseIntArray();
    private SparseArray<String> mNameArray;

    // 二级分类关联数据
    private List<Integer> mCateOther;

    private void initChooseDialog() {
        mCateOther = new ArrayList<>();
        mCateOther.add(PickCateType.TYPE_3);
        mCateOther.add(PickCateType.TYPE_31);
        mCateOther.add(PickCateType.TYPE_32);
        mCateOther.add(PickCateType.TYPE_33);
        mCateOther.add(PickCateType.TYPE_34);
        mCateOther.add(PickCateType.TYPE_35);
        mNameArray = new SparseArray<>();
        mNameArray.put(PickCateType.TYPE_1, "集采方式");
        mNameArray.put(PickCateType.TYPE_2, "集采状态");
        for (Integer integer : mCateOther) {
            mNameArray.put(integer, "集采类型");
        }
        mNameArray.put(PickCateType.TYPE_4, "报价客户");
        mNameArray.put(PickCateType.TYPE_5, "加盟客户");
        mNameArray.put(PickCateType.TYPE_6, "截至时间");
        mNameArray.put(PickCateType.TYPE_7, "省份");
        mNameArray.put(PickCateType.TYPE_8, "市区");
        mNameArray.put(PickCateType.TYPE_9, "会员等级");
        mChooseDialog = new XChooseDialog(getContext(), mNameArray);
        mChooseDialog.setCallBack(this);
        for (int i = 0; i < mNameArray.size(); i++) {
            int key = mNameArray.keyAt(i);
            addMainEntity(key, new ArrayList<>());
        }
    }

    public void setChooseItemData(int type, List<CommonPopEntity> data) {
        addMainEntity(type, data);
        switch (type) {
            case PickCateType.TYPE_1:
                mBinding.popMode.setData(data, mNo1);
                break;
            case PickCateType.TYPE_2:
                mBinding.popStatus.setData(data, mNo2);
                break;
            case PickCateType.TYPE_3:
                break;
            case PickCateType.TYPE_31:
                updateChildTabLayout(data, 0);
                break;
            case PickCateType.TYPE_6:
                mBinding.popTime.setData(data, mNo6);
                break;
        }
    }

    public void showLeftDialog(int type, List<CommonPopEntity> data) {
        mChooseDialog.addEntity(type, data);
        mChooseDialog.showDialog();
    }

    public void addMainEntity(int index, List<CommonPopEntity> list) {
        if (mData.get(index) != null) {
            if (list == null || list.size() == 0) {
                mData.get(index).setList(new ArrayList<>());
            } else {
                mData.get(index).setList(list);
            }
        } else {
            mData.put(index, new XChooseEntity(index, mNameArray.get(index), list));
        }
        mFilters.put(index, 0);
    }

    public void addMainEntity(int index, String name, List<CommonPopEntity> list) {
        if (!TextUtils.isEmpty(name)) {
            mNameArray.put(index, name);
        }
        if (mData.get(index) != null) {
            mData.get(index).setTypeName(mNameArray.get(index));
            if (list == null || list.size() == 0) {
                mData.get(index).setList(new ArrayList<>());
            } else {
                mData.get(index).setList(list);
            }
        } else {
            mData.put(index, new XChooseEntity(index, mNameArray.get(index), list));
        }
        mFilters.put(index, 0);
    }

    private boolean isListEmpty(int catType) {
        return mData.get(catType) != null &&
                (mData.get(catType).getList() == null || mData.get(catType).getList().size() == 0);
    }

    @Override
    public void loadChildData(XChooseEntity entity, int pos) {
        // 资质类型当前不需要联动
        switch (entity.getSort()) {
            case PickCateType.TYPE_31:
            case PickCateType.TYPE_32:
            case PickCateType.TYPE_33:
            case PickCateType.TYPE_34:
            case PickCateType.TYPE_35:
                updateMultiCategoryData(entity.getSort(), entity.getList().get(pos), pos, true);
                mChooseDialog.showDialog();
                break;
            case PickCateType.TYPE_7:
                updateCityData(entity.getList().get(pos), pos);
                mChooseDialog.showDialog();
                break;
            default:
                break;
        }
    }

    private final String[][] titles = new String[][]{
            new String[]{"商品类型", "类别", "品名"},
            new String[]{"机械分类", "机械类型", ""},
            new String[]{"设备分类", "设备类型", ""},
    };

    private void updateMultiCategoryData(int sort, CommonPopEntity entity, int pos, boolean isShowLeftDialog) {
        ArrayList<CommonPopEntity> objects = new ArrayList<>();
        String name1 = titles[category - 1][0];
        String name2 = titles[category - 1][1];
        String name3 = titles[category - 1][2];
        mData.get(PickCateType.TYPE_31).setTypeName(name1);
        mData.get(PickCateType.TYPE_32).setTypeName(name2);
        mData.get(PickCateType.TYPE_33).setTypeName(name3);
        for (int i = 0; i < mCateOther.size(); i++) {
            Integer integer = mCateOther.get(i);
            if (integer > sort) {
                if (isShowLeftDialog) {
                    mChooseDialog.addEntity(integer, objects);
                } else {
                    addMainEntity(integer, objects);
                }
            }
        }
        if (pos > 0 && mCateOther.contains(sort + 1)) {
            int index = mCateOther.indexOf(sort) + 1;
            if (entity.getChildren() != null && entity.getChildren().size() > 0) {
                if (!entity.getChildren().get(0).getTypeName().equals("全部") && !entity.getChildren().get(0).getTypeName().equals("不限")) {
                    entity.getChildren().add(0, new CommonPopEntity("", "全部"));
                }
                if (isShowLeftDialog) {
                    mChooseDialog.addEntity(mCateOther.get(index), entity.getChildren());
                } else {
                    addMainEntity(mCateOther.get(index), entity.getChildren());
                }
            } else {
                if (isShowLeftDialog) {
                    mChooseDialog.addEntity(mCateOther.get(index), objects);
                } else {
                    addMainEntity(mCateOther.get(index), objects);
                }
            }
        }
    }

    private void updateCityData(CommonPopEntity entity, int pos) {
        mChooseDialog.addEntity(PickCateType.TYPE_8, new ArrayList<>());
        if (pos > 0) {
            sModel.getAddressCityData(scrData, entity.getNo(), PickCateType.TYPE_8, true);
        }
    }

    private boolean isTabTwoDialogRefresh;

    @Override
    public void result(SparseArray<XChooseEntity> data, SparseIntArray map) {
        mData = CopyUtils.deepPopEntityCopy(data);
        mFilters = map.clone();
        isTabTwoDialogRefresh = true;
        for (int i = 0; i < mData.size(); i++) {
            XChooseEntity entity = mData.get(mData.keyAt(i));
            int key = entity.getSort();
            int value = mFilters.get(key);
            switch (key) {
                case PickCateType.TYPE_1:
                    mNo1 = entity.getItemNo(value);
                    mBinding.popMode.refreshChoose(mNo1, entity.getItemName(value), value);

                    break;
                case PickCateType.TYPE_2:
                    mNo2 = entity.getItemNo(value);
                    mBinding.popStatus.refreshChoose(mNo2, entity.getItemName(value), value);
                    break;
                case PickCateType.TYPE_31:
                    mNo31 = entity.getItemNo(value);
                    int tabCount = mBinding.childTab.getTabCount();
                    if (tabCount > 0) {
                        updateChildTabLayout(entity.getList(), value);
                    }
                    break;
                case PickCateType.TYPE_32:
                    mNo32 = entity.getItemNo(value);
                    break;
                case PickCateType.TYPE_33:
                    mNo33 = entity.getItemNo(value);
                    break;
                case PickCateType.TYPE_34:
                case PickCateType.TYPE_35:
                    if (value > 0) {
                        mNo33 = entity.getItemNo(value);
                    }
                    break;
                case PickCateType.TYPE_4:
                    mNo4 = entity.getItemNo(value);
                    break;
                case PickCateType.TYPE_5:
                    mNo5 = entity.getItemNo(value);
                    break;
                case PickCateType.TYPE_6:
                    mNo6 = entity.getItemNo(value);
                    mBinding.popTime.refreshChoose(mNo6, entity.getItemName(value), value);
                    break;
                case PickCateType.TYPE_7:
                    mNo7 = entity.getItemNo(value);
                    mBinding.popAddress.refreshChoose(mNo7, entity.getItemName(value), value);
                    mAddressPop.setDefPosition(value, 0);
                    break;
                case PickCateType.TYPE_8:
                    mNo8 = entity.getItemNo(value);
                    mAddressPop.setDefPosition(mFilters.get(PickCateType.TYPE_7), value);
                    if (value > 0) {
                        mBinding.popAddress.refreshChoose(mNo8, entity.getItemName(value), value);
                    }
                    break;
                case PickCateType.TYPE_9:
                    member_level = entity.getItemId(value);
                    break;
                default:
                    break;
            }
        }
        loadData(false);
    }

    @Override
    public void reset() {
        mFilters.clear();
        resetMainData();
        result(mData, mFilters);
        mChooseDialog.initData(mData, mFilters);
        mChooseDialog.showDialog();
    }

    @Override
    public void onCancel() {
        mChooseDialog.initData(mData, mFilters);
    }

    private void resetMainData() {
        if (mCurPos == 1) {
            for (Integer integer : mCateOther) {
                if (integer != PickCateType.TYPE_31) {
                    addMainEntity(integer, new ArrayList<>());
                }
            }
        } else {
            for (Integer integer : mCateOther) {
                addMainEntity(integer, new ArrayList<>());
            }
        }
        addMainEntity(PickCateType.TYPE_8, new ArrayList<>());
    }
}
